<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\PrizeDraw;
use think\Config;
use think\Queue;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        /*$re = $this->draw1();
        var_dump($re);exit();*/
        $site = Config::get('site.user_level');
        $this->success('请求成功');
    }

    protected function draw1(){

        $sum = 0;
        $listPoint = array(0);//这个数组记录了每个切割点的值，就是记录了数轴上，5,15,30,50,100的值。
        $seed = getUserLevel();
        foreach ($seed as $k => $v){

            $sum += $v;//计算出权值的总和
            array_push($listPoint, $sum);//把分割点放到数组中
        }
        //var_dump($listPoint);exit;
        //判断中奖人数
        $num = 1;


        $i = $zero = $one = $two = $three = $four = $five = $six =0;
        while ($i < 10000){
            $ids = $user_ids = $emptyLevel =  [];
            while (count($ids) < $num){

                //随机权重等级
                $level = $this->GetRandom($listPoint,$sum);

                switch ($level){
                    case 1;
                        $one++;
                        $ids = [1];
                        break;
                    case 2:
                        $two++;
                        $ids = [1];
                        break;
                    case 3:
                        $three++;
                        $ids = [1];
                        break;
                    case 4:
                        $four++;
                        $ids = [1];
                        break;
                    case 5:
                        $five++;
                        $ids = [1];
                        break;
                    case 6:
                        $six++;
                        $ids = [1];
                        break;
                }
            }
            $i++;
        }

        $data = [
            1 => $one,
            2 => $two,
            3 => $three,
            4 => $four,
            5 => $five,
            6 => $six,
        ];

        return $data;
    }

    /**
     * 抽奖逻辑
     * @param $list
     * @return array
     */

    protected function draw($list = [],$data = [])
    {
        //TODO 运算逻辑 抽取中奖记录
        //获取用户等级 获取种子数,所有prize_draw 生成 种子数 序列
        //打乱数组，rand ，取下表 或者 array_rand 取出
        //如果prize_draw_id 不在中奖名单中 则加入到名单数组
        //奖品总数 就是数组的长度

        $data['total'] = 10;

        $list = [];
        for($i=1;$i<=1000;$i++){
            $arr = [
                'id' => $i,
                'user' => [
                    'level' => 3,
                ]
            ];
            $list[] = $arr;
        }

        //用户幸运等级 对应种子个数
        $seed = Config::get('site.user_level');
        /*$seed = [
            1 => 1,
            2 => 3,
            3 => 9,
            4 => 18,
            5 => 36,
            6 => 72,
        ];*/

        $ids = [];
        //判断中奖人数
        $num = (count($list) > $data['total']) ? $data['total'] : count($list);

        //随机抽取 $num 个抽奖
        $t1 = microtime(true);

        $sum = 0;
        $listPoint = array(0);//这个数组记录了每个切割点的值，就是记录了数轴上，5,15,30,50,100的值。
        foreach ($list as $k => $v){
            $value = 1;
            if (isset($seed[$v['user']['level']])){
                $value = $seed[$v['user']['level']];
            }
            $sum += $value;//计算出权值的总和
            array_push($listPoint, $sum);//把分割点放到数组中
        }

        while (count($ids) < $num){
            $result = $this->GetRandom($listPoint,$sum);

            $id = $list[$result]['id'];

            //判断是否存在，否则加入中奖用户
            if (!in_array($id,$ids)){
                $ids[] = $id;
            }
        }
        $t2 = microtime(true);
        echo '耗时'.($t2-$t1).'<br>';
        echo '内存: ' . memory_get_usage()/(1024*1024) . '<br />';

        return $ids;
    }

    //返回一个随机中奖
    protected function GetRandom($listPoint,$sum){

        $k = mt_rand(0, $sum-1);//取0到sum之间一个随机值

        return $this->bin_sch($listPoint,0,count($listPoint),$k);
    }

    /**
     * 二分
     * @param $array
     * @param $low
     * @param $high
     * @param $k
     * @return int
     */
    protected function bin_sch($array,  $low, $high, $k){

        if ( $low <= $high){

            $mid =  intval(($low+$high)/2 );

            echo $mid;
            if ($k >= $array[$mid] && $k < $array[$mid + 1]){

                return $mid + 1;

            }elseif ( $k < $array[$mid]){

                return  $this->bin_sch($array, $low,  $mid-1, $k);

            }else{

                return  $this->bin_sch($array, $mid+ 1, $high, $k);

            }

        }

        return -1;

    }
}
